<template>
  <el-dialog title="新增" :visible.sync="visible" :show-close="false" width="400px" :modal="true" :close-on-click-modal="false" :close-on-press-escape="false">
    <button type="button" aria-label="Close" class="el-dialog__headerbtn" @click.stop="cancelModal"><i class="el-dialog__close el-icon el-icon-close"></i></button>
    <el-form class="form-wrapper" ref="orderForm" :model="orderForm" :rules="addRules" label-width="110px">
      <el-form-item label="代码：" prop="code">
        <el-input style="width:180px;" v-model="orderForm.code" type="text" placeholder="请输入代码"></el-input>
      </el-form-item>
      <el-form-item label="名称：" prop="name">
        <el-input style="width:180px;" v-model="orderForm.name" type="text" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="默认：" prop="checkboxvalue">
        <el-checkbox v-model="checkboxvalue"></el-checkbox>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
         <el-select :disabled="true" style="width:180px;" v-model="orderForm.status" placeholder="请选择">
                <el-option label="停用" value="2"></el-option>
                <el-option label="启用" value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="buttons-wrap">
      <el-button type="primary" @click="submitForm('orderForm')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
  export default {
    props: {
      visible: {
        type: Boolean,
        default: false
      }
    },
    data(){
      return {
        user: JSON.parse(window.sessionStorage.getItem("user")),
        checkboxvalue:'',
        orderForm: {
            code:'',
            name:'',
            xtmr:'',
            status:'1',
            zdr:'',
            qyr:''
        },
        addRules: {
          code: [{ required: true, message: "请输入代码", trigger: 'blur'}],
          name: [{ required: true, message: "请输入名称", trigger: 'blur'}],
        }
      }
    },
    methods: {
      initForm(){
        this.orderForm = {
            code:'',
            name:'',
            xtmr:'',
            status:'1',
            zdr:'',
            qyr:''
        };
        if(this.$refs.orderForm){
          this.$refs.orderForm.resetFields();
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              console.log(this.orderForm)
            //  确定时 对值进行转换 
            // this.orderForm.zdr =
            if(this.checkboxvalue){
                this.orderForm.xtmr = 1
            }else{
                this.orderForm.xtmr = 0
            }
            this.orderForm.zdr = this.user.name
            this.orderForm.qyr = this.user.name
            this.postRequest("/earnestmoney/sklx/create",this.orderForm).then(res => {
                console.log(res,'11111')
                if (res.status == '200') {
                    this.$parent.getdata();
                    this.$emit('update:visible', false);
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      cancelModal(){
        // 关闭弹窗，触发父组件修改visible值
        this.$emit('update:visible', false);
      }
    }
  }
</script>
<style lang="scss" scoped>
  .buttons-wrap {

    .el-button {
      margin-right: 20px;
      min-width: 100px;
    }
  }
</style>